//
//  UIImage+HHUtil.m
//  HHChatKit
//
//  Created by Henry on 2021/6/4.
//

#import "UIImage+HHUtil.h"
#import <HHTool/HHTool.h>
#import "HHResources.h"

@implementation UIImage (HHUtil)

+ (UIImage *)imageSenderWithColor:(UIColor *)color {
    UIImage *image = [HHResources getImageFromMessage:@"message_bubble_receiver"];
    if (@available(iOS 13.0, *)) {
    } else {
        image = [image hh_imageWithColor:color];
    }
    // 翻转
    image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored];
    // iOS13 颜色
    if (@available(iOS 13.0, *)) {
        image = [image imageWithTintColor:color];
    } else {
    }
    // 拉伸
    return [image resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{30,20,22,20}") resizingMode:UIImageResizingModeStretch];
}

+ (UIImage *)imageReceiverWithColor:(UIColor *)color {
    UIImage *image = [HHResources getImageFromMessage:@"message_bubble_receiver"];
    if (@available(iOS 13.0, *)) {
    } else {
        image = [image hh_imageWithColor:color];
    }
    // iOS13 颜色
    if (@available(iOS 13.0, *)) {
        image = [image imageWithTintColor:color];
    } else {
    }
    // 拉伸
    return [image resizableImageWithCapInsets:UIEdgeInsetsFromString(@"{30,22,22,22}") resizingMode:UIImageResizingModeStretch];
}

+ (UIImage *)getImage:(NSString *)name {
    UIColor *color = [self randomColor];  //获取随机颜色
    
    if (!name || name.length == 0) {
        color = [UIColor lightGrayColor];
    }
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 128, 128);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    if (!name || name.length == 0) {
        return img;
    }
    
    NSString *headerName = nil;
    if (name.length < 2) {
        headerName = name;
    } else {
        headerName = [name substringFromIndex:0];
    }
    UIImage *headerimg = [self imageToAddText:img withText:headerName];
    return headerimg;
}

/// 随机颜色
+ (UIColor *)randomColor {
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

/// 把文字绘制到图片上
+ (UIImage *)imageToAddText:(UIImage *)img withText:(NSString *)text {
    //1.获取上下文
    UIGraphicsBeginImageContext(img.size);
    //2.绘制图片
    [img drawInRect:CGRectMake(0, 0, img.size.width, img.size.height)];
    //3.绘制文字
    CGFloat mm = 15;
    CGRect rect = CGRectMake(mm, mm, img.size.width-mm*2, img.size.height-mm*2);
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
    style.alignment = NSTextAlignmentCenter;
    //文字的属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:80], NSParagraphStyleAttributeName:style, NSForegroundColorAttributeName:[UIColor whiteColor]};
    //将文字绘制上去
    [text drawInRect:rect withAttributes:dic];
    //4.获取绘制到得图片
    UIImage *watermarkImg = UIGraphicsGetImageFromCurrentImageContext();
    //5.结束图片的绘制
    UIGraphicsEndImageContext();
    return watermarkImg;
}

@end
